package com.seocoo.worker.order.widget.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.seocoo.worker.R;
import com.seocoo.worker.base.ImageLoaderEngine;
import com.seocoo.worker.order.entity.RspGoodsInfo;

/**
 * 订单详情明细适配器
 */
public class OrderDetailCommodityAdapter extends BaseQuickAdapter<RspGoodsInfo, BaseViewHolder> {

    private String status = "";

    public OrderDetailCommodityAdapter() {
        super(R.layout.order_detail_commodity_item);

    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    protected void convert(BaseViewHolder helper, RspGoodsInfo item) {
        //店铺图片
        ImageView ivCargoLogo = helper.getView(R.id.iv_cargo_logo);
        new ImageLoaderEngine(item.getCoverUrl()).error(R.drawable.order_detail_cargo_default).placeholder(R.drawable.order_detail_cargo_default).into(ivCargoLogo);
        if (item.isExpand()) {
            //当前是展开 箭头应该向上
            helper.setImageResource(R.id.iv_expand_goods, R.drawable.base_arrow_gray_up);
            helper.setGone(R.id.tv_content1, true);
        } else {
            //当前是收起状态 箭头应该向下
            helper.setImageResource(R.id.iv_expand_goods, R.drawable.base_arrow_gray_down);
            helper.setGone(R.id.tv_content1, false);
        }
        StringBuilder builder = new StringBuilder();
        String money1 = "";
        if (!TextUtils.isEmpty(item.getRecArea()) && !TextUtils.equals("0", item.getRecArea())) {
            money1 = "矩形框金额=" + item.getRectPrice() + "元/㎡×" + item.getRecArea() + "㎡;\n";
        }
        String money2 = "";
        if (!TextUtils.isEmpty(item.getAbnormityArea()) && !TextUtils.equals("0", item.getAbnormityArea())) {
            money2 = "异形框金额=" + item.getAlienPrice() + "元/㎡×" + item.getAbnormityArea() + "㎡;\n";
        }

        String money3 = "";
        if (!TextUtils.isEmpty(item.getCornerMeter()) && !TextUtils.equals("0", item.getCornerMeter())) {
            money3 = "转角金额=" + item.getAnglePrice() + "元/㎡×" + item.getCornerMeter() + "㎡;\n";
        }

        String money4 = "";
        if (!TextUtils.isEmpty(item.getGlassSpecification()) && !TextUtils.equals("0", item.getGlassSpecification())) {
            money4 = "玻璃金额=" + "（" + item.getRecArea() + "㎡+" + item.getAbnormityArea() + "㎡）×" + item.getGlassSpecificationDesc() + item.getGlassSpecification() + "元/㎡;\n";
        }
        String money5 = "";
        if (!TextUtils.isEmpty(item.getFanCount()) && !TextUtils.equals("0", item.getFanCount())) {
            money5 = "扇金额=" + item.getFanPrice() + "元/扇×" + item.getFanCount();
        }
        builder.append(money1)
                .append(money2)
                .append(money3)
                .append(money4)
                .append(money5);

        if (TextUtils.isEmpty(builder.toString())) {
            helper.setGone(R.id.iv_expand_goods, false);
//            helper.setGone(R.id.tv_content1, false);
        } else {
            helper.setGone(R.id.iv_expand_goods, true);
//            helper.setGone(R.id.tv_content1, true);
        }
        //营销方式1-用户推荐 2-折扣商品 3-平台推荐
        String sellType = "";
        String itemSellType = item.getSellType();
        if (TextUtils.equals(itemSellType, "1")) {
            sellType = "用户推荐";
        }
        if (TextUtils.equals(itemSellType, "2")) {
            sellType = "折扣商品";
        }
        if (TextUtils.equals(itemSellType, "3")) {
            sellType = "平台推荐";
        }

        if (TextUtils.equals(status, "101") || TextUtils.equals(status, "201")) {
            //orderStatus 等于101或者201的时候 商品展示参考价
            helper.setGone(R.id.cl2, true);
            helper.setGone(R.id.cl1, false);
            helper.setText(R.id.tv_area_money, item.getReferMinPrice() + " ~ " + item.getReferMaxPrice());
        } else {
            helper.setGone(R.id.cl2, false);
            helper.setGone(R.id.cl1, true);
            helper.setText(R.id.tv_area_money, "");
        }
        helper.addOnClickListener(R.id.iv_expand_goods);
        String otherCost = item.getOtherCost();
        if (TextUtils.isEmpty(otherCost) || TextUtils.equals("0", otherCost)) {
            helper.setGone(R.id.tv_other_money, false);
            helper.setGone(R.id.tv1, false);
        } else {
            helper.setGone(R.id.tv_other_money, true);
            helper.setGone(R.id.tv1, true);
        }

        helper.setText(R.id.tv_cargo_name, item.getGoodsName())
                .setText(R.id.tv_other_money, "￥" + otherCost)
                .setText(R.id.tv_content1, builder)
                .setText(R.id.tv_platform_recommend, sellType)
                .setText(R.id.tv_subtotal_money, item.getTotalMoney());

    }

    public void setGoodsExpand(int position, RspGoodsInfo item) {
        //true 表示当前展开 需要更改状态为收起  false 表示当前收起 需要更改状态为展开
        item.setExpand(!item.isExpand());
        notifyItemChanged(position, item);
    }
}
